﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace HR60_PanelSoft.View.Base
{

    //GC: [g1]bug2182/2183/2184[BaseFormControl]
        /*
         * 此次优化，使用自定义事件，区分事件源的类型，分别处理
         */
    //GC: [g2]优化：将核心的基础控件传递到外层[BaseFormControl]
    //GC: [g3]优化：优化valueChanged的迭代[BaseFormControl]
    //GC: [g4]优化：4bug2191[BaseFormControl]
        /*
         * 
         */

    /* [g1.s1]自定义事件和委托  */

    public enum EventType
    {
        NULL,
        GroupInPinItem,
        DeviceInPinItem,
        SubInPinItem,
        TimeInPinItem,
        TitleTextBox,
        FormTextBox,
    }
    public class ValueChangedEventArgs : EventArgs
    {
        public EventType Type = EventType.NULL;
        public object Tag = null;
        public ValueChangedEventArgs(EventType type)
        {
            Type = type;
        }

    }

    public delegate void ValueChangedHandler(object sender, ValueChangedEventArgs e); 
    /* end of [g1.s1] */

	/// <summary>
	/// Form控件基类，与BaseForm配合实现Form内的控件数据获取及自动提交
	/// 在BaseFormControl嵌套的情况下，只有顶层的Control有效
	/// </summary>
	public class BaseFormControl : BaseData
	{
		private string _Id = null;

		private object _Value = null;

		private bool _ValueEnabled = true;

		private bool _AutoSubmitFlag = true;

		private bool _CatchValueChange = true;

		private string _ValidRegx = "";

		private int _Min = -1;

		private int _Max = -1;

        /* [g3.s3]增加EventType字段 */
        private EventType _evtType = EventType.NULL;
        /* end of [g3.s3] */


        /* [g1.s2] */
		/// <summary>
		/// Value发生改变后的事件，并非所有控件都要实现这个机制，视情况而定
		/// </summary>
        [Browsable(true)]
        //public event EventHandler ValueChanged;
        public event ValueChangedHandler ValueChanged;


        /* end of [g1.s2] */

		/// <summary>
		/// 控件id，提交Form时作为Key
		/// </summary>
		[Browsable(true)]
		[Description("id"), Category("String"), DefaultValue("")]
		public virtual string Id
		{
			get
			{
				return this._Id;
			}
			set
			{
				this._Id = value;
			}
		}

		/// <summary>
		/// 控件取值，提交Form时作为Value
		/// </summary>
		public virtual object Value
		{
			get
			{
				return this._Value;
			}
			set
			{
				this._Value = value;
			}
		}

		/// <summary>
		/// 值是否有效，若无效则在提交时忽略此数据字段
		/// </summary>
		public virtual bool ValueEnabled
		{
			get
			{
				return _ValueEnabled;
			}
			set
			{
				_ValueEnabled = value;
			}
		}

		/// <summary>
		/// 是否自动提交的标志，如果为false则不会触发Form自动提交流程
		/// </summary>
		[Browsable(true)]
		[Description("AutoSubmitFlag"), Category("Boolean"), DefaultValue(true)]
		public bool AutoSubmitFlag
		{
			get
			{
				return this._AutoSubmitFlag;
			}
			set
			{
				this._AutoSubmitFlag = value;
			}
		}

		/// <summary>
		/// 是否触发ValueChange事件，若false，也不会触发Form自动提交流程
		/// </summary>
		[Browsable(true)]
		[Description("CatchValueChange"), Category("Boolean"), DefaultValue(true)]
		public bool CatchValueChange
		{
			get
			{
				return this._CatchValueChange;
			}
			set
			{
				this._CatchValueChange = value;
			}
		}

		/// <summary>
		/// 数据校验正则
		/// </summary>
		[Browsable(true)]
		[Description("ValidRegx"), Category("String"), DefaultValue("")]
		public virtual string ValidRegx
		{
			get
			{
				return this._ValidRegx;
			}
			set
			{
				this._ValidRegx = value;
			}
		}

		/// <summary>
		/// 字符串类型为最小长度，数值型为最小值
		/// </summary>
		[Browsable(true)]
		[Description("Min"), Category("Int32"), DefaultValue(-1)]
		public virtual int Min
		{
			get
			{
				return this._Min;
			}
			set
			{
				this._Min = value;
			}
		}

		/// <summary>
		/// 字符串类型为最大长度，数值型为最大值
		/// </summary>
		[Browsable(true)]
		[Description("Max"), Category("Int32"), DefaultValue(-1)]
		public virtual int Max
		{
			get
			{
				return this._Max;
			}
			set
			{
				this._Max = value;
			}
		}

        /* [g3.s2]增加EventType属性 */
        [Browsable(true)]
        [Description("Event Type"),Category("Enum"),DefaultValue(EventType.NULL)]
        public EventType EvtType
        {
            get
            {
                return this._evtType;
            }
            set
            {
                this._evtType = value;
            }
        }
        /* end of [g3.s2] */


		/// <summary>
		/// IFromControl的Value发生改变时触发执行
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        /* [g1.s3] */

        //protected virtual void valueChange(object sender, EventArgs e)
        
        protected virtual void valueChange(object sender, ValueChangedEventArgs e)
        /* end of [g1.s3] */
		{
			// 判断是否触发ValueChanged事件
			if (!CatchValueChange)
			{
				return;
			}
            if (ValueChanged != null )
            {
                ValueChanged(this, e);
            }
			
			// 判断是否执行自动提交流程
			if (!AutoSubmitFlag) return;

			Control control = (Control)sender;
			if (control.Parent == null) return;
            /* [g3.s4] */

            while (!control.Parent.GetType().IsSubclassOf(typeof(BaseForm)))
            {
                control = control.Parent;
                if (typeof(BaseFormControl).IsAssignableFrom(control.GetType()))
                {
                    //// 如果存在上一层BaseFormContrl，则传递触发valueChange，并返回
                    ///* [g3.s1]将sender改为当前控件 */
                    ((BaseFormControl)control).valueChange(this, e);
                    //((BaseFormControl)control).valueChange(this, new ValueChangedEventArgs(
                    //    ((BaseFormControl)control).EvtType));
                    ///* end of [g3.s1] */
                    return;
                    //continue;
                }
            }

            if (control.Parent == null) return;

            BaseForm form = (BaseForm)control.Parent;
            form.doFormChange((BaseFormControl)sender, e);

            /* 寻找顶级BaseFormControl */

            //Control tempTop = null;

            //while(!control.Parent.GetType().IsSubclassOf(typeof(BaseForm)))
            //{
            //    control = control.Parent;
            //    if (typeof(BaseFormControl).IsAssignableFrom(control.GetType()))
            //    {
            //        if (control.Parent == null)
            //            return;
            //        tempTop = control;
            //        continue;
            //    }
            //}

            

            //if (!(control.Parent is BaseForm) || tempTop == null)
            //    return;
            
            //BaseForm form = (BaseForm)control.Parent;
            //form.doFormChange((BaseFormControl)tempTop, e);

            /* end of [g3.s4] */
		}
        
        /* [g1.s4] */
        protected void valueChange_Normal(object sender, EventArgs e)
        {
            ValueChangedEventArgs vcea = new ValueChangedEventArgs(EventType.NULL);
            
            valueChange(sender, vcea);
        }
        /* end of [g1.s4] */

		/// <summary>
		/// 查找控件所在Form
		/// </summary>
		/// <returns></returns>
		protected BaseForm findForm()
		{
			Control control = this;
			while (!control.Parent.GetType().IsSubclassOf(typeof(BaseForm)))
			{
				control = control.Parent;
				if (control.Parent == null) return null;
			}
			return (BaseForm)control.Parent;
		}

		/// <summary>
		/// 查找控件所在FormControl
		/// </summary>
		/// <returns></returns>
		protected BaseFormControl findParentFormControl()
		{
			Control control = this;
			while (!control.Parent.GetType().IsSubclassOf(typeof(BaseFormControl)))
			{
				control = control.Parent;
				if (control.Parent == null) return null;
			}
			return (BaseFormControl)control.Parent;
		}
		/// <summary>
		/// 查找控件列表
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public IList<BaseFormControl> findChildFormControls()
		{
			return findChildFormControls(null);
		}


		/// <summary>
		/// 通过id查找控件列表
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public IList<BaseFormControl> findChildFormControls(string id)
		{
			IList<BaseFormControl> list = new List<BaseFormControl>();
			BaseForm.findChildFormControls(id, list, this);
			return list;
		}

        /* [g2.s1] */
        /// <summary>
        /// 获取核心子控件列表，传递到外层
        /// 核心子控件检索范围：子层至基础控件层；
        /// </summary>
        /// <returns>核心子控件列表</returns>
        public virtual List<Control> GetCoreControls() { return null; }

        /// <summary>
        /// 根据名称获取子控件，传递到外层
        /// 子控件检索范围；子层
        /// </summary>
        /// <param name="name">子控件名称</param>
        /// <returns></returns>
        public Control GetSubControlsByName(string name) 
        {
            Control rst = null;
            foreach (Control c in this.Controls)
            {
                if (c.Name == name)
                    return c;
            }
            return rst; 
        }
        /* end of [g2.s1] */
	}
}
